<?php

/* Product Status Note:
  1: available
  2: new
  3: out of stock
  4: coming soon
  5: sale off
  6: hide
 */

include_once('../dao/productDAO.php');

function getProducts($keyword, $categoryID, $start_position, $row_per_page) {
    return loadProducts($keyword, $categoryID, $start_position, $row_per_page);
}

function getNumberOfResult($keyword, $categoryID) {
    return countNumberOfResult($keyword, $categoryID);
}

function getProductByID($productID) {
    return loadProductByID($productID);
}

function getAvailableRelatedProductByID($proID) {
    return loadAvailableRelatedProductByID($proID);
}

function getRelatedProductByID($proID) {
    return loadRelatedProductByID($proID);
}

function getRelatedProductByKeyword($keyword, $id) {
    return loadAvailableRelatedProductByKeyword($keyword, $id);
}

function addProduct($cateID, $proCode, $proName, $proPrice, $proDescription, $proStatus) {
    return insertProduct($cateID, $proCode, $proName, $proPrice, $proDescription, $proStatus);
}

function editProduct($proID, $cateID, $proCode, $proName, $proPrice, $proDescription, $proStatus) {
    return updateProduct($proID, $cateID, $proCode, $proName, $proPrice, $proDescription, $proStatus);
}

function removeRelatedProduct($proID) {
    return deleteAllRelatedProduct($proID);
}

function addRelatedProduct($proID, $pro_pro_id) {
    return insertRelatedProduct($proID, $pro_pro_id);
}

function getCurrentProID() {
    $row = selectCurrentProID();
    return $row['MAX_ID'];
}

function removeProduct($proID) {
    $imagePath = $imagePath = "../img/product/" . $proID . "/";
    deleteProductRelationship($proID);
    deleteProduct($proID);

    if (is_dir($imagePath))
        deleteDir($imagePath);
}

function deleteDir($dir) {
    if (substr($dir, strlen($dir) - 1, 1) != '/')
        $dir .= '/';

    if ($handle = opendir($dir)) {
        while ($obj = readdir($handle)) {
            if ($obj != '.' && $obj != '..') {
                if (is_dir($dir . $obj)) {
                    if (!deleteDir($dir . $obj))
                        return false;
                }
                elseif (is_file($dir . $obj)) {
                    if (!unlink($dir . $obj))
                        return false;
                }
            }
        }

        closedir($handle);

        if (!@rmdir($dir))
            return false;
        return true;
    }
    return false;
}

function getNumberOfProduct() {
    return countTotalProduct();
}

function getTopProduct() {
    return loadTopProduct();
}

function getProductsByCateAndStatus($status, $categoryID, $start_position, $row_per_page) {
    return loadProductByCateAndStatus($status, $categoryID, $start_position, $row_per_page);
}

function getProductsByName($name, $start_position, $row_per_page) {
    return loadProductsByNames($name, $start_position, $row_per_page);
}

function getNumberOfProductByCateAndStatus($status, $categoryID) {
    return coutNumberOfResultByStatusAndCate($status, $categoryID);
}

function getNumProductsByCategory($catID){
    return countNumProductsByCategory($catID);
}

function getProductByName($name){
    return findProductByName($name);
}

function getNumberOfProductByName($name){
    return countNumberofProductByName($name);
}

function getProductByBanner($banID){
    return findProductByBanner($banID);
}

function getAllProduct() {
    return loadAllProduct();
}
?>